<?php
/**
 * 个人设置
 */
namespace api\home\controller;

use cmf\controller\RestBaseController;
use think\Db;
use think\Validate;
use EasyWeChat\Factory;
use think\Config;

class SetupController extends RestBaseController
{

    public $Token;
    public $UserId;
    public function __construct(){
        $this->data = input();
        $token =  input('token') ? input('token') : $_SERVER['HTTP_TOKEN'];
        if(!$token){
            $this->error('请先登陆！');
        }
        $session = Db::table('a_user')->where(['session'=>$token])->value('session');
        if(!$session){
            echo json_encode(array('code'=>'90001','msg'=>'token已过期'));die;
        }
        $this->Token = $token;
        $this->UserId = substr($this->Token,12);
    }
    
    /**
     * [rpass 修改密码]
     * @return [type] [description]
     */
    public function rpass(){
        if (!isset($this->data['orderps'])||empty($this->data['orderps'])) {
            $this->error('请填写旧密码！');
        }
        if (!isset($this->data['newpass'])||empty($this->data['orderps'])) {
            $this->error('请填写新密码！');
        }
        if (!isset($this->data['rnewpass'])||empty($this->data['orderps'])) {
            $this->error('请填再次填写新密码！');
        }
        $o = Db::table('a_user')->where(['id'=>$this->UserId])->value('password');
        if(md5($this->data['orderps']) != $o){
            $this->error('旧密码有误！');
        }
        if($this->data['newpass'] != $this->data['rnewpass']){
            $this->error('两次密码不一致！');
        }
        $rq = Db::table('a_user')->where(['id'=>$this->UserId])->update(['password'=>md5($this->data['newpass'])]);
        if($rq){
            $this->success('修改成功！');
        }
        $this->error('修改失败！');
    }   
}